package cn.xx.pet.service.impl;

import cn.xx.basic.domain.Point;
import cn.xx.basic.exception.CustomException;
import cn.xx.basic.service.impl.BaseServiceImpl;
import cn.xx.basic.util.DistanceUtil;
import cn.xx.org.domain.Employee;
import cn.xx.org.domain.Shop;
import cn.xx.org.mapper.ShopMapper;
import cn.xx.pet.domain.MasterMsg;
import cn.xx.pet.mapper.MasterMsgMapper;
import cn.xx.pet.mapper.PetTypeMapper;
import cn.xx.pet.service.IMasterMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MasterMsgServiceImpl extends BaseServiceImpl<MasterMsg> implements IMasterMsgService {

    @Autowired
    private MasterMsgMapper masterMsgMapper;

    @Autowired
    private PetTypeMapper petTypeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Override
    public void publish(MasterMsg masterMsg) throws CustomException {
        //1.发布消息的用户为空(controller 里面解决)
        //2.计算离用户最近的门店
        /*
            2.1 把用户发布的地址转换为 经纬度
            2.2 把所有的门店加载出来 把所有的地址 都转换为经纬度
            2.3 求发布人与 门店之间的距离（计算 球面上两个点之间的距离）

         */
        //用户发布地址的经纬度
        Point point = DistanceUtil.getPoint(masterMsg.getAddress());
        //计算最近的门店
        List<Shop> shops = shopMapper.findAll();
        Shop shop = DistanceUtil.getNearestShop(point, shops);
        masterMsg.setShop_id(shop.getId());
        //3.默认的状态 待处理
        masterMsg.setState(0);
        super.save(masterMsg);
    }

    @Override
    public Employee findByLogin(Long id) {
        return masterMsgMapper.findByLogin(id);
    }

}
